<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import router from "@/router";
import myCookies from "@/utils/myCookies";
import { ElMessage } from "element-plus";
import { LoginApl } from "@/api/login/login";
import { useCounterStore } from "@/stores/counter";
import Cookies from "js-cookie";

//表单验证规则
interface RuleForm {
  username: string;
  password: string;
}
const Loginform = ref<RuleForm>({
  username: "",
  password: "",
});
const loginLoading = ref(false);
const ruleFormRef = ref<FormInstance>();
const rules = reactive({
  username: [{ required: true, message: "未输入账号", trigger: ["blur", "change"] }],
  password: [{ required: true, message: "未输入密码", trigger: ["blur", "change"] }],
});

//记住账号密码
const isRememberUserAndPass = ref(false);
const load = () => {
  isRememberUserAndPass.value =
    localStorage.getItem("isRememberUserAndPass") === "true" ? true : false;
  Loginform.value.username = localStorage.getItem("username") || "";
  Loginform.value.password = localStorage.getItem("password") || "";
};
watch(isRememberUserAndPass, (newValue, oldValue) => {
  if (newValue) {
    localStorage.setItem("isRememberUserAndPass", "true");
  } else {
    localStorage.setItem("isRememberUserAndPass", "false");
    localStorage.removeItem("username");
    localStorage.removeItem("password");
  }
});
const loginsumbit = async (formEl: FormInstance | undefined) => {
  loginLoading.value = true;
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await LoginApl(Loginform.value).then(async (response) => {
        if (response.data.code === 200) {
          //设置token
          myCookies.setToken(response.data.data.token);
          //设置缓存用户信息
          localStorage.setItem("userInfo", JSON.stringify(response.data.data.user));
          //消息提示
          ElMessage({
            message: "登录成功！",
            type: "success",
          });

          // 加载个人信息
          localStorage.setItem("userInfo", JSON.stringify(response.data.data.user));
          if (isRememberUserAndPass.value) {
            localStorage.setItem("isRememberUserAndPass", "true");
            localStorage.setItem("username", Loginform.value.username);
            localStorage.setItem("password", Loginform.value.password);
          }

          router.push("/home");
        }
      });
    } else {
      console.log("error submit!", fields);
    }
    loginLoading.value = false;
  });
};
load();
</script>
<template>
  <div class="login-main">
    <div class="logincontent"></div>
    <div class="login-box">
      <div class="logo-title">
        <img src="../assets/logo.png" />
        <h1>花购平台</h1>
      </div>

      <el-card class="loginCard">
        <el-form :model="Loginform" ref="ruleFormRef" :rules="rules">
          <el-row>
            <el-col :span="24">
              <h1 style="text-align: center; font-family: cursive">登录</h1>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="username">
                <el-input
                  v-model="Loginform.username"
                  placeholder="账号/手机号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="password">
                <el-input v-model="Loginform.password" show-password placeholder="密码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-checkbox
                  v-model="isRememberUserAndPass"
                  label="记住账号密码"
                  size="large"
                  style="font-family: cursive"
                />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item>
                <el-button
                  :loading="loginLoading"
                  style="width: 460px"
                  type="danger"
                  @click="loginsumbit(ruleFormRef)"
                  >{{ loginLoading ? "登录中..." : "立即登录" }}</el-button
                >
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-link target="primary">忘记密码</el-link>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-link href="/register" target="primary" style="margin-left: auto"
                  >立即注册</el-link
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<style scoped lang="less">
.logincontent {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw; /* 视口宽度 */
  height: 100vh; /* 视口高度 */
  background-size: cover; /* 关键属性：覆盖整个容器 */
  background-position: center; /* 图片居中显示 */
  background-repeat: no-repeat;
  z-index: -1; /* 确保内容在上层 */
  filter: blur(8px); /* 标准语法 */
  background-image: url("../assets/花购平台登录背景图.png");
}
.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh; /* 视口高度 */

  min-height: 600px;
  .loginCard {
    border-radius: 20px;
    text-align: center;
    width: 500px;
    background-color: aliceblue;
  }
}
.logo-title {
  width: 500px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  img {
    display: inline-block;
    width: 80px;
  }
  h1 {
    display: inline-block;
    color: #ff0060;
    font-family: cursive;
  }
}
.login-main {
  min-width: 1000px;
}
</style>
